package com.example.aitoedu.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.aitoedu.entity.Student;
import com.example.aitoedu.entity.Teacher;
import com.example.aitoedu.entity.TsTalk;
import com.example.aitoedu.entity.User;
import com.example.aitoedu.mapper.*;
import com.example.aitoedu.service.TsTalkService;
import com.example.aitoedu.vo.AllUserTalkVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 曹正豪
* @description 针对表【ts_talk(学生和老师对话内容信息)】的数据库操作Service实现
* @createDate 2024-12-27 20:50:56
*/
@Service
public class TsTalkServiceImpl extends ServiceImpl<TsTalkMapper, TsTalk>
    implements TsTalkService{

    @Autowired
    private TsTalkMapper tsTalkMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private ScMapper scMapper;

    @Override
    public List<AllUserTalkVO> allConversation() {
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        List<TsTalk> tsTalks = tsTalkMapper.allConversation(id);
        if (tsTalks == null) return null;
        User userRole = userMapper.selectById(id);
        List<List<TsTalk>> lists = new ArrayList<>();
        List<AllUserTalkVO> allUserTalkVOList = new ArrayList<>();
        if(userRole.getRole() == 1){
            List<Long> tids = scMapper.getAllTidsBySid(id);
            for (Long tid : tids) {
                List<TsTalk> collect = tsTalks.stream()
                        .filter(tsTalk -> tsTalk.getSender() == tid || tsTalk.getReceiver() == tid)
                        .collect(Collectors.toList());
                lists.add(collect);
            }
            for(int i = 0; i < tids.size(); i++){
                Teacher teacher = teacherMapper.selectById(tids.get(i));
                AllUserTalkVO allUserTalkVO = AllUserTalkVO.builder()
                        .talker(teacher.getId())
                        .talkName(teacher.getTname())
                        .picture(teacher.getPicture())
                        .build();
                if(lists.get(i).isEmpty()){
                    allUserTalkVOList.add(allUserTalkVO);
                    continue;
                }
                allUserTalkVO.setLastTime(lists.get(i).get(lists.get(i).size()-1).getCreateTime());
                allUserTalkVO.setLastContent(lists.get(i).get(lists.get(i).size()-1).getContent());
                Integer count = 0;
                for (TsTalk list : lists.get(i)){
                    if (list.getIsRead() == 0 && list.getReceiver() == id) count++;
                }
                allUserTalkVO.setNotRead(count);
                allUserTalkVOList.add(allUserTalkVO);
            }
        }
        if(userRole.getRole() == 2){
            List<Long> sids = scMapper.getAllSidsByTid(id);
            for (Long sid : sids) {
                List<TsTalk> collect = tsTalks.stream()
                        .filter(tsTalk -> tsTalk.getSender() == sid || tsTalk.getReceiver() == sid)
                        .collect(Collectors.toList());
                lists.add(collect);
            }
            for(int i = 0; i < sids.size(); i++){
                Student student = studentMapper.selectById(sids.get(i));
                AllUserTalkVO allUserTalkVO = AllUserTalkVO.builder()
                        .talker(student.getId())
                        .talkName(student.getSname())
                        .picture(student.getPicture())
                        .build();
                if(lists.get(i).isEmpty()){
                    allUserTalkVOList.add(allUserTalkVO);
                    continue;
                }
                allUserTalkVO.setLastTime(lists.get(i).get(lists.get(i).size()-1).getCreateTime());
                allUserTalkVO.setLastContent(lists.get(i).get(lists.get(i).size()-1).getContent());
                Integer count = 0;
                for (TsTalk list : lists.get(i)){
                    if (list.getIsRead() == 0 && list.getReceiver() == id) count++;
                }
                allUserTalkVO.setNotRead(count);
                allUserTalkVOList.add(allUserTalkVO);
            }
        }
        List<AllUserTalkVO> collect1 = allUserTalkVOList.stream()
                .filter(c1 -> c1.getLastTime()!=null)
                .sorted(Comparator.comparing(AllUserTalkVO::getLastTime).reversed()
                        .thenComparing(AllUserTalkVO::getNotRead).reversed())
                .collect(Collectors.toList());
        List<AllUserTalkVO> collect2 = allUserTalkVOList.stream()
                .filter(c2 -> c2.getLastTime() == null)
                .collect(Collectors.toList());
        collect1.addAll(collect2);
        return collect1;
    }

    @Override
    public List<TsTalk> onlyConversation(Long id) {
        Long myId = Long.valueOf(StpUtil.getLoginId().toString());
        List<TsTalk> tsTalks = tsTalkMapper.selectByTwoId(myId,id);
        this.setRead(id);
        return tsTalks;
    }

    @Override
    public void setRead(Long id) {
        Long myId = Long.valueOf(StpUtil.getLoginId().toString());
        tsTalkMapper.setRead(myId,id);
    }
}




